package cn.edu.mju.property.dao.impl;

import cn.edu.mju.property.dao.RepairDao;
import cn.edu.mju.property.entity.Repair;
import cn.edu.mju.property.entity.User;
import cn.edu.mju.property.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class RepairDaoImpl implements RepairDao {

    @Override
    public boolean addRepair(Repair repair) {
        return false;
    }

    @Override
    public boolean delRepair() {
        return false;
    }

    @Override
    public boolean updateRepair(Repair repair) {
        return false;
    }


    /*分页查询方法*/
    @Override
    public List<Repair> findByPage(int start, int rows) {
        Repair repair = null;
        List<Repair> Repairlist = new ArrayList<Repair>();
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from repair limit ? , ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,start);
            pstmt.setInt(2,rows);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                repair = new Repair();
                repair.setId(rs.getInt("id"));
                repair.setUsername(rs.getString("username"));
                repair.setProject(rs.getString("project"));
                repair.setPhone(rs.getString("phone"));
                repair.setTime(rs.getString("time"));
                Repairlist.add(repair);
            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Repairlist;
    }

    @Override
    public int findTotalCount() {

        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select count(*) from repair";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            int num =  rs.getInt("count(*)");
            pstmt.close();
            conn.close();
            return num;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


}
